import { Pipe, PipeTransform } from '@angular/core';


/**
 *  10000 to 10,000
 */
@Pipe({
    name: 'numberTrans',
})
export class NumberTransform implements PipeTransform {
    /**
     * 数字格式转换
     * @param value
     * @returns {string}
     */
    transform(value,typeNum=0) {
        if(value==0){
            return '0'
        }
        let value1,value2;
        if( value && Number(value).toString()!='NaN' ){
            value1 = value;
            if(value.indexOf('-')!=-1){
                value1 = value1.slice(1);
                console.log(value1)
            }
            if(value1.indexOf('.')!=-1){
                value2 = value.split('.')[1];
                value1 = value.split('.')[0];
            }
            let i;
            if(typeNum==1){
              i='';
            }else if(typeNum == 0){
               i=0;
            }else{
                console.log(typeNum);
                return typeNum;
            }
            let num = (value1 || i).toString(), result = '';
            while (num.length > 3) {
                result = ',' + num.slice(-3) + result;
                num = num.slice(0, num.length - 3);
            }
            if (num) {
                result = num + result;
            }
            if(value.indexOf('.')!=-1){
                if(value.indexOf('-')!=-1){
                    return '-'+result+'.'+value2;
                }else{
                    return result+'.'+value2;
                }
            }else{
                if(value.indexOf('-')!=-1){
                    return '-'+result;
                }else{
                    return result;
                }
            }
        }else{
            if(typeNum==1){
                return ''
            }else if(typeNum == 0){
                return 0
            }else{
                return typeNum;
            }
        }
    }
}
